package com.antrain.vuedemo.controller;

import java.util.Map;

import javax.annotation.Resource;

import com.antrain.vuedemo.entity.User;
import com.antrain.vuedemo.service.IUserService;
import com.antrain.vuedemo.utils.Result;
import com.antrain.vuedemo.utils.ResultGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author antrain
 * @since 2020-08-13
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Resource
    private IUserService userService;
    

    /**
     * 初始化分页信息
     */
    private void initPage(Map<String,Object> param){
        if(StringUtils.isEmpty(param.get("page"))){
            param.put("page", 1);
        }
        if(StringUtils.isEmpty(param.get("limit"))){
            param.put("limit", 3);
        }
    }

    @GetMapping()
    public Result getList(@RequestParam Map<String,Object> param) {
        initPage(param);
        int num = Integer.parseInt(param.get("page").toString());
        int limit = Integer.parseInt(param.get("limit").toString());
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.like("username", param.get("username"));
        IPage<User> page = new Page<>(num, limit);//参数一是当前页，参数二是每页个数
        return ResultGenerator.getSuccessResult(userService.page(page, wrapper));
    }

    @GetMapping("/{id}")
    public Result getUser(@PathVariable int id) {
        User user = userService.getById(id);
        if (user == null) return ResultGenerator.getFailResult("","无用户记录");
        return ResultGenerator.getSuccessResult(user);
    }

    @PostMapping()
    public Result save(@RequestBody User user){
        //System.out.println(user);
        if(userService.save(user)) return ResultGenerator.getSuccessResult("","插入成功");
        return ResultGenerator.getFailResult("","插入失败");
    }

    @PutMapping("/{id}")
    public Result update(@RequestBody User user,@PathVariable int id){
        user.setId(id);
        //System.out.println(user);
        if(userService.updateById(user)) return ResultGenerator.getSuccessResult("","更新成功");
        return ResultGenerator.getFailResult("","更新失败");
    }

    @DeleteMapping("/{id}")
    public Result del(@PathVariable int id) {
        if(userService.removeById(id)) return ResultGenerator.getSuccessResult("","删除成功");
        return ResultGenerator.getFailResult("","删除失败");
    }
}
